package com.iman.manjusri.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.constraints.NotNull;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.common.support.JSONPage;
import com.iman.manjusri.domain.CourseCommentBean;
import com.iman.manjusri.domain.CourseCountBean;
import com.iman.manjusri.domain.CourseInfoBean;
import com.iman.manjusri.domain.CourseNoteBean;
import com.iman.manjusri.domain.CourseTypeBean;
import com.iman.manjusri.domain.TopicCourseBean;
import com.iman.manjusri.domain.User;
import com.iman.manjusri.service.CourseCommentService;
import com.iman.manjusri.service.CourseCountService;
import com.iman.manjusri.service.CourseInfoService;
import com.iman.manjusri.service.CourseNoteService;
import com.iman.manjusri.service.CourseTypeService;
import com.iman.manjusri.service.UserService;
import com.iman.manjusri.vo.CourseAssistInfoVo;
import com.iman.manjusri.vo.CourseClassmateVo;
import com.iman.manjusri.vo.CourseCommentVo;
import com.iman.manjusri.vo.CourseCtgVo;
import com.iman.manjusri.vo.CourseDetailVo;
import com.iman.manjusri.vo.CourseJieShaoVo;
import com.iman.manjusri.vo.CourseKeShiVo;
import com.iman.manjusri.vo.CourseNoteVo;
import com.iman.manjusri.vo.CourseRecommendVo;
import com.iman.manjusri.vo.CourseTeacherVo;

@Controller
@RequestMapping(value = "/course/*")
public class CourseController extends BaseController {
   
	@Autowired
	CourseInfoService courseInfoService;
	
	@Autowired
	CourseCountService courseCountService;
	@Autowired
	UserService classMateService;
	
	@Autowired
	CourseCommentService courseCommentService;
	
	@Autowired
	CourseNoteService courseNoteService;
	
	@Autowired
	CourseTypeService  courseTypeService;
	@Autowired
	UserService userService;
	@RequestMapping(value = "/getCourse")
	public ModelAndView getCourseDetail(@NotNull @RequestParam(required = true)  String courseId){
		Map<String,Object> result = new  HashMap<String,Object>();
		
		ModelAndView view = null;
		
		CourseDetailVo courseDetailVo = new CourseDetailVo();
		courseDetailVo.setCourseTitle("cource_title");
		courseDetailVo.setCourseDetail("course_detail");
		courseDetailVo.setCourseEndTime("courseEndTime");
		courseDetailVo.setCourseId("courseId");
		courseDetailVo.setCourseLocation("location");
		courseDetailVo.setCourseOffPrice("offprice");
		courseDetailVo.setCoursePeopleCount("coursePeopleCount");
		courseDetailVo.setCoursePic("kczx_son_kc01.jpg");
		courseDetailVo.setCoursePrice("coursePrice");
		courseDetailVo.setCourseShortDesc("courseShortDesc");
		courseDetailVo.setCourseStartTime("courseStartTime");
		courseDetailVo.setCourseTeacher("course_teacher");
		
		result.put("courseDetail", courseDetailVo);
		
		
		view = new ModelAndView("course_detail",result);
		putResourceSrc(view);	
		return view;
   }
	
	@RequestMapping(value = "/getPublicCourse")
	public ModelAndView getPublicCourse(){
		Map<String,Object> result = new  HashMap<String,Object>();
		
		ModelAndView view = null;
		//先查询所有course_type=1 的信息
		Map<String,Object> courseClassMap = new HashMap<String,Object>();
		courseClassMap.put("course_class", 1);
		List<CourseTypeBean> courseTypeBeanList = courseTypeService.courseTypeListBySearchMap(courseClassMap);
		List<Long>  courseTypeList = new ArrayList<Long>();
		for(CourseTypeBean bean:courseTypeBeanList){
			courseTypeList.add(bean.getId());
		}
		//根据类型信息便利出所有课程信息
		Map<String,Object> searchCourseTypeMap = new HashMap<String,Object>();
		if(courseTypeList.size()>0){
			searchCourseTypeMap.put("courseTp", courseTypeList);
			
		}
		List<CourseRecommendVo> courseRecommendVoList = courseInfoService.getRecommendPublicCourseInfoBeanList(searchCourseTypeMap);
		
		
		result.put("courseRecommendVoList", courseRecommendVoList);
		
		
		view = new ModelAndView("public_course",result);
		putResourceSrc(view);	
		return view;
   }
	
	@RequestMapping(value = "/getCourseCenter")
	public ModelAndView getCourseCenter(){
		Map<String,Object> result = new  HashMap<String,Object>();
		
		ModelAndView view = null;
		
		
		
		//推荐课程 4条
		List<CourseRecommendVo> courseRecommendList = new ArrayList<CourseRecommendVo>();
		Map<String,Object> offsetMap = new HashMap<String,Object>();
		offsetMap.put("offset", 4);
		courseRecommendList = courseInfoService.getRecommendCourse(offsetMap);
		result.put("recommendCourse", courseRecommendList);
		
		//获取课程分类
		//查询条件：course_class=2
		Map<String,Object> courseClassMap = new HashMap<String,Object>();
		courseClassMap.put("course_class", 2);
		List<CourseTypeBean> courseTypeBeanList = courseTypeService.courseTypeListBySearchMap(courseClassMap);
		//获取list课程
		List<Long>  courseTypeList = new ArrayList<Long>();
		for(CourseTypeBean bean:courseTypeBeanList){
			courseTypeList.add(bean.getId());
		}
		//根据类型信息便利出所有课程信息
		Map<String,Object> searchCourseTypeMap = new HashMap<String,Object>();
		if(courseTypeList.size()>0){
			searchCourseTypeMap.put("courseTp", courseTypeList);
			
		}
		List<CourseRecommendVo> courseRecommendVoList = courseInfoService.getRecommendPublicCourseInfoBeanList(searchCourseTypeMap);
		
		
		result.put("courseRecommendVoList", courseRecommendVoList);
		
		
		result.put("courseTypeBeanList", courseTypeBeanList);
		view = new ModelAndView("course_center",result);
		putResourceSrc(view);	
		return view;
   }
	
  /**
   * 获取课程信息
   * @param  courseId 课程id
   * @param  channelInfo 频道信息 赋值信息：gkk  kczx topic
   * */
  @RequestMapping(value = "/getCourseInfo")
  public ModelAndView getCourseInfo(@NotNull @RequestParam(required = true)  String courseId,@NotNull @RequestParam(required = true)  String channelInfo){
	  Map<String,Object> result = new  HashMap<String,Object>();
	  ModelAndView view = null;
	  //课程辅助信息
	  CourseAssistInfoVo courseAssistInfoVo = new CourseAssistInfoVo();
	  //课程细节信息
	  CourseDetailVo courseDetailVo = new CourseDetailVo();
	  //教师信息
	  //推荐课程
	  CourseRecommendVo recommendVo = new CourseRecommendVo();
	  CourseTeacherVo courseTeacherVo = new CourseTeacherVo();
	  Map<String,Object> searchMap = new HashMap<String,Object>();
	  searchMap.put("courseId", courseId);
	  CourseInfoBean courseInfoBean = courseInfoService.getCourseInfo(searchMap);
	  CourseCountBean courseCountBean = courseCountService.getCourseCountBeanById(Long.parseLong(courseId));
	  
	  int courseOnlineTp = 1;//课程方式  1：线上课程 2：线下课程
	  if(courseCountBean != null){
		  courseAssistInfoVo.setCourseCommentNum(courseCountBean.getCourseCommentcnt());//评论次数
		  
		  courseAssistInfoVo.setCourseId(NumberUtils.toInt(courseId, 0));//课程id
		  courseAssistInfoVo.setCoursePurchaseNum(courseCountBean.getCourseBuycnt());//购买数量
		  courseAssistInfoVo.setCourseScore(3.80);//课程分数
		  courseAssistInfoVo.setCourseTimes(15);//课时
		  courseAssistInfoVo.setCourseVisitNum(courseCountBean.getCourseBrowsercnt());//浏览次数
		  
		  
		  
	  }
	  
	  
	  
	  
	  courseDetailVo.setCourseEndTime("课程结束时间");
	  courseDetailVo.setCourseId(courseId);//课程id
	  courseDetailVo.setCourseLocation("上课地点");
	  courseDetailVo.setCoursePic("");//课程图片
	 
	  if(courseInfoBean != null){
		  courseDetailVo.setCourseTitle(courseInfoBean.getCourseNm());//课程标题
		  courseDetailVo.setCoursePrice("￥"+1.00*courseInfoBean.getOrgPrice()/100);
		  courseDetailVo.setCourseOffPrice("￥"+1.00*courseInfoBean.getCurPrice()/100);
		  courseTeacherVo.setTeacherId(courseInfoBean.getCourseTeacherid());//教师ID
		  
		  
		  //获取课程方式
		  courseOnlineTp =  courseInfoBean.getCourseOnlinetp();
		  courseDetailVo.setCourseDetail(courseInfoBean.getCourseSmart());
	  }
	  
	  
	  Map<String,Object> map = new HashMap<String,Object>();
	  map.put("userId", courseInfoBean.getCourseTeacherid());
	  //getUserById
	  User teacherUser = userService.getUserById(map);
	  if(teacherUser != null){
		  courseTeacherVo.setTeacherDesc(teacherUser.getUser_desc());
		  courseTeacherVo.setTeacherImg(teacherUser.getUser_himg());//教师图片
		  courseTeacherVo.setTeacherName(teacherUser.getUserName());//教师名称
		  
	  }
	  
	  
	  List<CourseRecommendVo> courseRecommendList = new ArrayList<CourseRecommendVo>();
	  if(courseOnlineTp !=2){
		  Map<String,Object> offsetMap = new HashMap<String,Object>();
		  offsetMap.put("offsetMap", 10);
		  courseRecommendList = courseInfoService.getRecommendCourse(offsetMap);
		  
	  }
	  
	  List<CourseCtgVo> courseCtgVoList = courseInfoService.getCourseCtgVoInfo(searchMap);
	  result.put("courseDetail", courseDetailVo);
	  result.put("recommendCourse", courseRecommendList);
	  result.put("courseTeacher", courseTeacherVo);
	  result.put("courseAssistInfo", courseAssistInfoVo);
	  result.put("courseKeShiVo", courseCtgVoList);
	  if("gkk".equals(StringUtils.trimToEmpty(channelInfo))){
		  if(courseOnlineTp == 2){
			  view = new ModelAndView("ms_son",result);
		  }else if(courseOnlineTp == 1){
			  view = new ModelAndView("gkk",result);
			  
		  }
		  
	  }else if("kczx".equals(StringUtils.trimToEmpty(channelInfo))){
		  if(courseOnlineTp == 2){
			  view = new ModelAndView("ms_son",result);
		  }else if(courseOnlineTp == 1){
			  view = new ModelAndView("kczx",result);
			  
		  }
		  //view = new ModelAndView("kczx",result);
	  }else if("topic".equals(StringUtils.trimToEmpty(channelInfo))){
		  Map<String,Object> topicMap = new HashMap<String,Object>();
		  topicMap.put("courseId", courseId);
		  topicMap.put("subjectId", courseId);
		  List<TopicCourseBean> topicCourseList  = courseInfoService.getTopicInfoList(topicMap);
		  result.put("topicCourseList", topicCourseList);
		  view = new ModelAndView("zt",result);
	  }
	  putResourceSrc(view);	
	  return view;
  }
  
  
  /**
   * 获取推荐课程list
   * 
   * */
  @RequestMapping(value = "/getRecommendCourseList")
  public ModelAndView getRecommentCourseList(){
	  Map<String,Object> result = new  HashMap<String,Object>();
	  ModelAndView view = null;
	  
	  //推荐课程
	  CourseRecommendVo recommendVo = new CourseRecommendVo();
	  Map<String,Object> map = new HashMap<String,Object>();
	  List<CourseRecommendVo> courseRecommendList = new ArrayList<CourseRecommendVo>();
	  Map<String,Object> offsetMap = new HashMap<String,Object>();
	  offsetMap.put("offsetMap", 10);
	  courseRecommendList = courseInfoService.getRecommendCourse(offsetMap);
	  result.put("recommendCourse", courseRecommendList);
	 
	  view = new ModelAndView("recommendCourseList",result);
	  
	  putResourceSrc(view);	
	  return view;
  }
  
  
  /***
   * 获取课时信息
   * @param courseId 课程id
   * @param rows 条数
   * @param pages 页数
   * */
  @RequestMapping(value = "/keshilist")
	@ResponseBody()
	public JSONPage listCourseKeShi(@RequestParam(required = false) String courseId,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) Integer page) throws Exception {
	    List<CourseKeShiVo> courseKeShiVoList = new ArrayList<CourseKeShiVo>();
	    
	    CourseKeShiVo courseKeshiVo = new CourseKeShiVo();
	    courseKeshiVo.setCourseId(0);
	    courseKeshiVo.setKeShiId(1);//课时id
	    courseKeshiVo.setKeShiTitle("三级心理测验技能知识点");
	    courseKeshiVo.setKeShiType(1);
	    
	    CourseKeShiVo courseKeshiVo1 = new CourseKeShiVo();
	    courseKeshiVo1.setCourseId(0);
	    courseKeshiVo1.setKeShiId(1);//课时id
	    courseKeshiVo1.setKeShiTitle("三级心理测验技能知识点");
	    courseKeshiVo1.setKeShiType(2);
	    
	    courseKeShiVoList.add(courseKeshiVo);
	    courseKeShiVoList.add(courseKeshiVo1);
		JSONPage jsonpage = new JSONPage();
		//jsonpage.setPage(page);
		jsonpage.setPagesize(10);
		jsonpage.setRecords(55);
		jsonpage.setRows(courseKeShiVoList);
		jsonpage.setTotal(6);
		return jsonpage;
	}
  
  
  /**
   * 获取评论列表
   *  @param courseId 课程id
   *  @param rows 行数
   *  @param page 页数
   * */
  @RequestMapping(value = "/commentlist")
	@ResponseBody()
	public JSONPage listCourseComment(@RequestParam(required = false) String courseId,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) Integer page) throws Exception {
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("courseId", courseId);
	    List<CourseCommentBean> courseCommentList = courseCommentService.getCourseCommentList(map);
	    int totalSize = courseCommentService.getCourseCommentListCount(map);
	    List<CourseCommentVo> commentList = new ArrayList<CourseCommentVo>();
	    for(CourseCommentBean bean:courseCommentList){
	    	CourseCommentVo courseCommentVo = new CourseCommentVo();
		    courseCommentVo.setCommentContent(StringUtils.trimToEmpty(bean.getCommontTxt()));
		    courseCommentVo.setCommentDate(bean.getCommontCdt());
		    courseCommentVo.setCommentId(bean.getId()); 
		    courseCommentVo.setCourseId(Long.parseLong(courseId));
		    courseCommentVo.setNickName(bean.getUserRnm());
		    courseCommentVo.setUserId(bean.getCommontUid());
		    courseCommentVo.setCommentType(1);
		    commentList.add(courseCommentVo);
	    }
	    
		JSONPage jsonpage = new JSONPage();
		//jsonpage.setPage(page);
		jsonpage.setPagesize(10);
		jsonpage.setRecords(totalSize);
		jsonpage.setRows(commentList);
		jsonpage.setTotal(totalSize/10+1);
		return jsonpage;
	}
	
  
  /**
   * 获取公共课程json
   * */
  @RequestMapping(value = "/listRecommendCourseByType")
  @ResponseBody()
  public JSONPage listRecommendCourseByType(@RequestParam(required = false) String typeName,
		@RequestParam(required = false) Integer rows,
		@RequestParam(required = false) Integer page)throws Exception{
	    Map<String,Object> map = new HashMap<String,Object>();
	    if(StringUtils.isNotBlank(typeName)){
	    	typeName = new String(typeName.getBytes("iso8859-1"),"UTF-8");
	    	map.put("course_tp", typeName);
	    	
	    }
	    map.put("course_class", 1);
	    //获取类型list
	    List<CourseTypeBean> courseTypeBeanList = courseTypeService.courseTypeListBySearchMap(map);
	    JSONPage jsonpage = new JSONPage();
	    if(courseTypeBeanList != null && courseTypeBeanList.size()>0){
	    	CourseTypeBean courseTypeBean = (CourseTypeBean)courseTypeBeanList.get(0);
	    	Map<String,Object> searchCourseTypeMap = new HashMap<String,Object>();
	    	List<Long>  courseTypeList = new ArrayList<Long>();
			for(CourseTypeBean bean:courseTypeBeanList){
				courseTypeList.add(bean.getId());
			}
	    	if(courseTypeList.size()>0){
				searchCourseTypeMap.put("courseTp", courseTypeList);
				
			}
			List<CourseRecommendVo> courseRecommendVoList = courseInfoService.getRecommendPublicCourseInfoBeanList(searchCourseTypeMap);
			
			
			
	    	jsonpage.setRecords(courseRecommendVoList.size());
	    	jsonpage.setRows(courseRecommendVoList);
	    	jsonpage.setTotal(courseRecommendVoList.size()/rows+1);
	    }else{
	    	jsonpage.setRecords(0);
	    	jsonpage.setRows(new ArrayList<CourseInfoBean>());
	    	jsonpage.setTotal(1);
	    	
	    }
	    jsonpage.setPagesize(page);
		return jsonpage;
  }
  
  /**
   * 获取公共课程json
   * */
  @RequestMapping(value = "/listRecommendCourseByTypeAndSubType")
  @ResponseBody()
  public JSONPage listRecommendCourseByTypeAndSubType(@RequestParam(required = false) String typeId,@RequestParam(required = false) String subType,
		@RequestParam(required = false) Integer rows,
		@RequestParam(required = false) Integer page)throws Exception{
	    Map<String,Object> map = new HashMap<String,Object>();
	    if(StringUtils.isNotBlank(typeId)){
	    	typeId = new String(typeId.getBytes("iso8859-1"),"UTF-8");
	    	map.put("id", typeId);
	    	
	    }
	    map.put("course_class", 2);
	    //获取类型list
	    List<CourseTypeBean> courseTypeBeanList = courseTypeService.courseTypeListBySearchMap(map);
	    JSONPage jsonpage = new JSONPage();
	    if(courseTypeBeanList != null && courseTypeBeanList.size()>0){
	    	CourseTypeBean courseTypeBean = (CourseTypeBean)courseTypeBeanList.get(0);
	    	Map<String,Object> searchCourseTypeMap = new HashMap<String,Object>();
	    	List<Long>  courseTypeList = new ArrayList<Long>();
			for(CourseTypeBean bean:courseTypeBeanList){
				courseTypeList.add(bean.getId());
			}
	    	if(courseTypeList.size()>0){
				searchCourseTypeMap.put("courseTp", courseTypeList);
				
			}
			List<CourseRecommendVo> courseRecommendVoList = courseInfoService.getRecommendPublicCourseInfoBeanList(searchCourseTypeMap);
			
			
			
	    	jsonpage.setRecords(courseRecommendVoList.size());
	    	jsonpage.setRows(courseRecommendVoList);
	    	jsonpage.setTotal(courseRecommendVoList.size()/rows+1);
	    }else{
	    	jsonpage.setRecords(0);
	    	jsonpage.setRows(new ArrayList<CourseInfoBean>());
	    	jsonpage.setTotal(1);
	    	
	    }
	    jsonpage.setPagesize(page);
		return jsonpage;
  }
  
  /**
   * 笔记列表
   * @param courseId 课程id
   * @param rows 行数
   * @param page 页数
   * 
   * */
	
   @RequestMapping(value = "/notelist")
	@ResponseBody()
	public JSONPage listCourseNote(@RequestParam(required = false) String courseId,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) Integer page) throws Exception {
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("courseId", courseId);
	    List<CourseNoteBean> courseNoteList = courseNoteService.getCourseNoteList(map);
	    List<CourseNoteVo> noteList = new ArrayList<CourseNoteVo>();
	    for(CourseNoteBean bean:courseNoteList){
	    	CourseNoteVo courseNoteVo = new CourseNoteVo();
		    courseNoteVo.setCourseId(Integer.parseInt(courseId));
		    courseNoteVo.setNoteContent(bean.getNoteTxt());
		    courseNoteVo.setNoteDate(new Date());
		    courseNoteVo.setNoteTitle(bean.getNoteNm());
		    courseNoteVo.setUserId(bean.getUserId());
		    courseNoteVo.setUserNickName(bean.getUserName());
		    courseNoteVo.setUserPic(bean.getUserImg());
		    noteList.add(courseNoteVo);
	    }
	    
	    
	    
	    
		JSONPage jsonpage = new JSONPage();
		//jsonpage.setPage(page);
		jsonpage.setPagesize(10);
		jsonpage.setRecords(55);
		jsonpage.setRows(noteList);
		jsonpage.setTotal(6);
		return jsonpage;
	}
   
   
   
  /**
   * 获取同学信息
   * @param courseId 课程id
   * @param rows 行数
   * @param page 页数
   * */
   
   @RequestMapping(value = "/courseClassmatelist")
	@ResponseBody()
	public JSONPage listCourseClassmate(@RequestParam(required = false) String courseId,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) Integer page) throws Exception {
	    Map<String,Object> map = new HashMap<String,Object>();
	    map.put("courseId", courseId);
	    List<User> userList = userService.getUserList(map);
	    List<CourseClassmateVo> classmateList = new ArrayList<CourseClassmateVo>();
	    for(User user:userList){
	    	CourseClassmateVo classmateVo = new CourseClassmateVo();
	    	classmateVo.setCourseId(NumberUtils.toLong(courseId, 0));
	    	classmateVo.setUserId(""+user.getUserId());
	    	classmateVo.setUserNickName(user.getLoginName());
	    	classmateVo.setUserPic(user.getUser_himg());
	    	classmateList.add(classmateVo);
	    }
	    
		JSONPage jsonpage = new JSONPage();
		//jsonpage.setPage(page);
		jsonpage.setPagesize(10);
		jsonpage.setRecords(55);
		jsonpage.setRows(classmateList);
		jsonpage.setTotal(6);
		return jsonpage;
	}
   
   
   
   /**
    * 获取课程结束信息
    * @param courseId 课程id
    * @param rows 条数信息
    * @param page 页数信息
    * */
   @RequestMapping(value = "/courseJieShao")
	@ResponseBody()
	public JSONPage listCourseJieShao(@RequestParam(required = false) String courseId,
			@RequestParam(required = false) Integer rows,
			@RequestParam(required = false) Integer page) throws Exception {
	   
	    
	    
	    Map<String,Object> searchMap = new HashMap<String,Object>();
		searchMap.put("courseId", courseId);
		CourseInfoBean courseInfoBean = courseInfoService.getCourseInfo(searchMap);
	    
	    Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", courseInfoBean.getCourseTeacherid());
		//getUserById
		User teacherUser = userService.getUserById(map);
		
		CourseJieShaoVo classJieShaoVo = new CourseJieShaoVo();
	    if(courseInfoBean != null){
	    	classJieShaoVo.setCourseDetail(courseInfoBean.getCourseDesc());
	    	classJieShaoVo.setTeacherId(courseInfoBean.getCourseTeacherid());
	    	classJieShaoVo.setTeacherImg(teacherUser.getUser_himg());
	    	classJieShaoVo.setTeacherName(teacherUser.getUserName());
	    	
	    }
	    
	    
		
	    
	    List<CourseJieShaoVo> courseJieShaoList = new ArrayList<CourseJieShaoVo>();
	    courseJieShaoList.add(classJieShaoVo);
		JSONPage jsonpage = new JSONPage();
		//jsonpage.setPage(page);
		jsonpage.setPagesize(10);
		jsonpage.setRecords(55);
		jsonpage.setRows(courseJieShaoList);
		jsonpage.setTotal(6);
		return jsonpage;
	}
}
